const express=require('express')
const router=express.Router()
const pool=require('../pool');
const query = require('../router/utils')
const {   
    resSuccessData,
    resSuccess,
    resError} = require('../utils/index')
const {host} = require('../router/host')
const multer=require('multer')

router.post('/list',(req,res)=>{
    pool.query('select * from zhuanji_c',(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+'/'+el.photo
        });
      res.send({code:1,data:result});
    })
})
//删除专辑分类接口
router.post('/del',async(req,res)=>{
    // 传入数组
    let dels = req.body;
    // console.log(dels)
    if(dels.length){
        dels = dels.join(',');
        // 删除
        let sql = `delete from zhuanji_c where id in(${dels})`;
        const ret = await query(sql,[]);
        if(ret.affectedRows>0){
            resSuccess(res)
        }else{
            resError(res,'删除失败')
        }
    }else{
        resError(res,'删除参数不能为空')
    }
})
// 上传专辑封面图片的接口
// 修改磁盘存储规则
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'image/zhuanji/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})
var upload2 = multer({ storage: storage })
// 上传封面图片
router.post('/addFile', upload2.single('file'),async (req, res) => {
    // body 包含表单信息
    const photo = req.file.path.replace(/\\/g, '/').slice(6);
    // 将这个数据返回给前端,前端上传查到数据
    res.send(photo)

})
// 增加专辑分类
router.post('/add',async (req,res)=>{
    let {name,photo}=req.body
    let sql='insert into zhuanji_c (name,photo) values (?,?)';
    let ret=await query(sql,[name,photo])
    if(ret.affectedRows>0){
        res.send({code:1,msg:'增加成功'})
    }
})
// 编辑专辑分类
router.post('/zhuanjiDetail',async (req,res)=>{
    let id=req.body.id
    let sql="select * from zhuanji_c where id=?"
    let ret =await query(sql,[id])
    if(ret.length){
        res.send({code:1,data:ret[0]})
    }else{
        res.send({code:0,msg:'查找失败'})
    }
})
// 编辑后的专辑上传
router.post('/update',async (req,res)=>{
    let {name,photo,id}=req.body
    let sql='update zhuanji_c set name=?,photo=? where id=?'
    let ret=await query(sql,[name,photo,id])
    if(ret.affectedRows>0){
        resSuccess(res)
    }else{
        resError(res,'修改失败')
    }
    
})

// 编辑后的专辑下的乐谱上传
router.post('/detailDetail',async(req,res)=>{
    let {id}=req.body
    let sql='select d.id,d.song,d.photo,d.z_id from zhuanji as d inner join zhuanji_c as c on d.z_id=c.id where d.id=?'
    let ret=await query(sql,[id])
    if(ret.length){
        res.send({code:1,data:ret[0]})
    }else{
        res.send({code:0,msg:'查找失败'})
    }
})

// 获得全部专辑歌曲
router.post('/detailList',(req,res)=>{
    let page=req.body.page
    let keywords=req.body.keywords
    let condition=''
    let params=[]
    if(keywords){
        condition=`where d.song like '%${keywords}%'`
    }
    let count=6
    let start=(parseInt(page)-1)*count
    let sql=`select d.id,d.song,d.photo,c.name from zhuanji as d inner join zhuanji_c as c on d.z_id=c.id ${condition} limit ${start},${count} `
    pool.query(sql,params,(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+"/"+el.photo
        });
        if(keywords){
            condition=`where song like '%${keywords}%'`
        }
        let sql=`select count(id) as count from zhuanji ${condition}`
        pool.query(sql,[],(err,result1)=>{
            if(err) throw err
            res.send({code:1,data:result,total:result1[0].count})
        })
    })
})
// 上传专辑分类下的乐谱图片
var storage2 = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'image/zhuanjiDetail/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})
var upload3 = multer({ storage: storage2 })
router.post('/addDetailFile', upload3.single('file'),async (req, res) => {
    // body 包含表单信息
    const photo = req.file.path.replace(/\\/g, '/').slice(6);
    // 将这个数据返回给前端,前端上传查到数据
    res.send(photo)

})

// 专辑下的乐谱添加
router.post('/detailAdd',async(req,res)=>{
    let {song,photo,z_id}=req.body
    let sql='insert into zhuanji (song,photo,z_id) values (?,?,?)';
    let ret=await query(sql,[song,photo,z_id])
    if(ret.affectedRows>0){
        res.send({code:1,msg:'增加成功'})
    }
})
// 删除乐谱
router.post('/detailDel',async (req,res)=>{
    // 传入数组
    let dels = req.body;
    // console.log(dels)
    if(dels.length){
        dels = dels.join(',');
        // 删除
        let sql = `delete from zhuanji where id in(${dels})`;
        const ret = await query(sql,[]);
        if(ret.affectedRows>0){
            resSuccess(res)
        }else{
            resError(res,'删除失败')
        }
    }else{
        resError(res,'删除参数不能为空')
    }
})
// 专辑分类下的曲谱修改上传
router.post('/detailUpdate',async(req,res)=>{
    let {song,photo,z_id,id}=req.body
    let sql='update zhuanji set song=?,photo=?,z_id=? where id=?'
    let ret=await query(sql,[song,photo,z_id,id])
    if(ret.affectedRows>0){
        resSuccess(res)
    }else{
        resError(res,'修改失败')
    }
    
})
module.exports=router